﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ServiceModel;
using WCFServiceWebRole1.Services;
using System.Threading;
using System.ServiceModel.Channels;
using System.Diagnostics;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Text;
//using WCFServiceWebRole1.Services;
//using System.ServiceModel.Web;

namespace WCFServiceWebRole1_Tests
{
    [TestClass]
    public class UnitTest4
    {
        private static WebServiceHost host;

        private static Iusers proxy;
        private static void HostWithWebServiceHost(Uri baseAddress)
        {


            host = new WebServiceHost(typeof(user));
           // host.AddDefaultEndpoints();
            WebHttpBinding binding = new WebHttpBinding();
            host.AddServiceEndpoint(typeof(Iusers), binding, baseAddress);
            host.Open();

            //Console.WriteLine(@"go to http://localhost:8000/WebServiceHost/SomeOperation to test");
            //Console.ReadLine();
           // host.Close();

        }
        [ClassInitialize]
        public static void InitializeClass(TestContext ctx)
        {
         //   Uri baseAddress = new Uri("http://localhost:80/Services/users.svc");
           // HostWithWebServiceHost(baseAddress);


            host = new WebServiceHost(typeof(user));
            //, new Uri("http://localhost:8000/Services/"));
            host.Open();
            proxy = CreateProxy();
           // UnitTest4.myServiceHost_ =

            //    new WebServiceHost(typeof(user),new Uri("http://localhost:8080/Services/users.svc"));
            //ServiceEndpoint ep = myServiceHost_.AddServiceEndpoint(typeof(Iusers), new WebHttpBinding(), "http://localhost:8080/Services/users.svc");
            //ServiceDebugBehavior stp = myServiceHost_.Description.Behaviors.Find<ServiceDebugBehavior>();
            //stp.HttpHelpPageEnabled = true;
            ////myServiceHost_.AddServiceEndpoint(typeof(Iusers),
            ////                    new WebHttpBinding(), "http://localhost:8080/Services/users.svc");

          



           // Thread.Sleep(TimeSpan.FromMinutes(1));

        }
        [ClassCleanup()]
        public static void cleanup(){
          //  host.Close();
    }
        private static Iusers CreateProxy()
        {
            //EndpointAddress ep = new EndpointAddress("users.svc");
          //  ServiceEndpoint ep = host.AddServiceEndpoint(typeof(Iusers), new WebHttpBinding(), "");
            //var binding = new CustomBinding(
            //    new TextMessageEncodingBindingElement(MessageVersion.None, Encoding.UTF8),
            //    new HttpTransportBindingElement { ManualAddressing = true }
            //    );

            var factory = new ChannelFactory<Iusers>(new WebHttpBinding(), "http://localhost:8000/Services/users.svc");
            factory.Endpoint.Behaviors.Add(new WebHttpBehavior());

            return factory.CreateChannel();
        }
        [TestMethod]
        public void FinalTest()
        {
                  
            //using (System.ServiceModel.Web.WebChannelFactory<Iusers> factory = new WebChannelFactory<Iusers>("local"))
            //{
            //    Iusers socialChannel = factory.CreateChannel();
            //    Assert.Equals(socialChannel.getUserByUserId("1092").response.Username.ToString(), "ayyappan");
            //}
           // using (ChannelFactory<Iusers> factory = new ChannelFactory<Iusers>(new WebHttpBinding(), "http://localhost:80/Services/users.svc"))
           // {
             //   factory.Endpoint.Behaviors.Add(new WebHttpBehavior());

//                Iusers proxy = factory.CreateChannel();

                // Debug.Print(proxy.getDevice(1092,1060).response.Creation_date.ToString());
                try
                {
                    Assert.Equals(proxy.getUserByUserId("1092").response.Username.ToString(), "ayyappan");
                }
                catch (CommunicationException commProblem)
                {
                    Console.WriteLine("There was a communication problem. " + commProblem.Message + commProblem.StackTrace);
                    Console.Read();
                }
                // Assert.Equals(proxy.CheckUserName("ayyappan"), "true");

           // }
        }
    }
}
